//
//  selfInfoViewController.m
//  忘不了
//
//  Created by rimi on 16/7/14.
//  Copyright © 2016年 handuan. All rights reserved.
//

#import "selfInfoViewController.h"
#import "SDAutoLayout.h"
#import "UserModel.h"
#import "changeEmailViewController.h"
#import "changePasswordViewController.h"
#import "BaseRequest+MoneyAndRemember.h"
#import "UIImageView+WebCache.h"
@interface selfInfoViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *titleArray;
@property (nonatomic,strong)UIImageView *headImage;
@property (nonatomic,strong)NSString *imageUrl;
@end

@implementation selfInfoViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    UserModel *user = [UserModel shareUserModel];
    if (user.isSan) {
        [_headImage sd_setImageWithURL:[NSURL URLWithString:user.image]];
    }else{
        [BaseRequest getHeadImageFromUserName:[UserModel shareUserModel].userNmae completionhanlder:^(Response *response) {
            if (response.isSuccess) {
                NSLog(@"获取头像成功");
                _imageUrl = response.resultObject;
                NSLog(@"头像%@",_imageUrl);
                [_headImage sd_setImageWithURL:[NSURL URLWithString:_imageUrl]];
            }else{
                NSLog(@"获取头像失败");
                _headImage.image = [UIImage imageNamed:@"head"];
            }
        }];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"个人信息";
    
    UIImageView *backGroudView = [[UIImageView alloc]initWithFrame:self.view.frame];
    backGroudView.image = [UIImage imageNamed:@"s.jpg"];
    [self.view addSubview:backGroudView];
    
    [self loadUI];
    
}

- (void)loadUI {
    _headImage = [[UIImageView alloc]init];
    _headImage.backgroundColor = [UIColor grayColor];
    _headImage.layer.cornerRadius = self.view.frame.size.width*0.4/2.0;
    _headImage.layer.masksToBounds = YES;
    [self.view addSubview:_headImage];
    _headImage.sd_layout
    .centerXIs(self.view.frame.size.width/2)
    .topSpaceToView(self.view,self.view.frame.size.width*0.2)
    .widthIs(self.view.frame.size.width*0.4)
    .heightEqualToWidth();
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(10, self.view.frame.size.width*0.6+20, self.view.frame.size.width-20, 150) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.layer.cornerRadius = 15;
    _tableView.layer.masksToBounds = YES;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 50;
    _tableView.scrollEnabled = NO;
    [self.view addSubview:_tableView];
    
    _titleArray = [NSMutableArray array];
    _titleArray = @[@"账号",@"修改邮箱",@"修改密码"].mutableCopy;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifer = @"cell";
    UserModel *user = [UserModel shareUserModel];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifer];
    }
    if (indexPath.row == 0) {
        cell.detailTextLabel.text = [UserModel shareUserModel].userNmae;
    }
    cell.textLabel.text = _titleArray[indexPath.row];
    if (indexPath.row != 0) {
        if (user.isSan) {
            cell.userInteractionEnabled = NO;
            cell.textLabel.textColor = [UIColor colorWithRed:0.4762 green:0.4762 blue:0.4762 alpha:0.2];
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
}

//选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 1) {
        changeEmailViewController *changeEmailVC = [[changeEmailViewController alloc]init];
        [self.navigationController pushViewController:changeEmailVC animated:YES];
    }
    if (indexPath.row == 2) {
        changePasswordViewController *changePasswordVC = [[changePasswordViewController alloc]init];
        [self.navigationController pushViewController:changePasswordVC animated:YES];
    }
}


@end
